//
//  IJPhotosAuthorization.m
//  iwjw
//
//  Created by ChenJianjun on 2017/2/4.
//  Copyright © 2016年 iwujwu. All rights reserved.
//

#import "IJPhotosAuthorization.h"
#import <Photos/Photos.h>

@implementation IJPhotosAuthorization

+ (void)showActionSheetWithCameraBlock:(void (^)())cameraBlock
                            photoBlock:(void (^)())photoBlock
{
    //是否有相册的权限
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                [self showAlertActionWithCameraBlock:cameraBlock
                                          photoBlock:photoBlock];
            }
        }];
    } else if (status == PHAuthorizationStatusRestricted
               || status == PHAuthorizationStatusDenied) {
        [self showSettingAlertWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        return;
    }
    
    //是否有相机的权限
    AVAuthorizationStatus authStatusCamera = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatusCamera == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                [self showAlertActionWithCameraBlock:cameraBlock
                                          photoBlock:photoBlock];
            }
        }];
    } else if (authStatusCamera == AVAuthorizationStatusRestricted
               || authStatusCamera == AVAuthorizationStatusDenied) {
        [self showSettingAlertWithSourceType:UIImagePickerControllerSourceTypeCamera];
        return;
    }
    
    [self showAlertActionWithCameraBlock:cameraBlock
                              photoBlock:photoBlock];
}

+ (BOOL)isAllAuthorizationsOpen
{
    AVAuthorizationStatus authStatusCamera = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    PHAuthorizationStatus authStatusPhoto = [PHPhotoLibrary authorizationStatus];
    if (authStatusCamera == AVAuthorizationStatusAuthorized
        && authStatusPhoto == PHAuthorizationStatusAuthorized) {
        return YES;
    }
    return NO;
}

+ (void)showAlertActionWithCameraBlock:(void (^)())cameraBlock
                            photoBlock:(void (^)())photoBlock
{
    if (![self isAllAuthorizationsOpen]) {
        return;
    }
    UIAlertController *actionSheetVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (cameraBlock) {
            cameraBlock();
        }
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (photoBlock) {
            photoBlock();
        }
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [actionSheetVC addAction:action0];
    [actionSheetVC addAction:action1];
    [actionSheetVC addAction:action2];
    [UIViewController ks_presentViewController:actionSheetVC animated:YES completion:nil];
}

+ (void)showSettingAlertWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
    NSString *message = @"";
    switch (sourceType) {
        case UIImagePickerControllerSourceTypePhotoLibrary:
            message = @"请在“设置-隐私-照片”中，允许访问你的手机相册";
            break;
        case UIImagePickerControllerSourceTypeCamera:
            message = @"请在“设置-隐私-相机”中，允许访问你的相机";
            break;
        default:
            break;
    }
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
        }
    }];
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    [UIViewController ks_presentViewController:alertVC animated:YES completion:nil];
}

@end
